package cc.lj.internal.coupons.domain.ticket.service.impl.useTicketRule;

import cc.lj.internal.api.ordershare.dto.OrderOnlineAPIDTO;
import cc.lj.internal.core.exception.ServiceException;
import cc.lj.internal.coupons.domain.ticket.model.vo.TicketBatchDO;
import cc.lj.internal.coupons.domain.ticket.model.vo.TicketMainDO;
import cc.lj.internal.coupons.domain.ticket.service.AbstractUseTicketRuleDomainService;
import cc.lj.internal.coupons.infrastructure.excepion.CoupousExceptionEnum;
import cc.lj.internal.coupons.interfaces.dto.command.UseTicketCheckCommand;
import lj.internal.dict.enums.ticketBatch.EnumTicketGiveStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 用券验证规则 - 转赠
 * @author liangxifeng
 * @date 2023/6/16 15:12
 */
@Service("useTicketGiveRuleDomainServiceImpl")
@Slf4j
public class UseTicketGiveRuleDomainServiceImpl extends AbstractUseTicketRuleDomainService {
    @Override
    protected void rule(TicketBatchDO batchDO, TicketMainDO ticketMainDO, UseTicketCheckCommand ticketCheckCommand, OrderOnlineAPIDTO orderOnlineAPIDTO) {
        log.info("【电子券用券验证】：券转赠规则验证开始==");
        //验证券是否可以转赠 , 券不可转赠
        if(batchDO.getAstrictRule() == EnumTicketGiveStatus.NO.getCode() ) {
            //发送对象为空
            if(ticketMainDO.getTicketSendObject() == null || ticketMainDO.getTicketSendObject().trim().isEmpty()){
                log.error("券转赠规则验证：券批次主键:"+batchDO.getBatchId()+",电子券使用码="+ticketCheckCommand.getTicketUseCode()+",该券不可转赠，券发送对象字段为空,不可用券");
                throw new ServiceException(CoupousExceptionEnum.TICKET_SEND_OBJEDT_ISNULL);
            }
            //发送对象 ！= 订单中的手机号
            if( !ticketMainDO.getTicketSendObject().equals(orderOnlineAPIDTO.getCustomerPhone())) {
                log.error("券转赠规则验证：券批次主键:"+batchDO.getBatchId()+",电子券使用码="+ticketCheckCommand.getTicketUseCode()+
                        ",该券不可转赠，券发送对象"+ticketMainDO.getTicketSendObject()+" ！= 下单时用户手机号:"+orderOnlineAPIDTO.getCustomerPhone()+"不可用券");
                throw new ServiceException(CoupousExceptionEnum.TICKET_ASTRICT_RULE_ERROR);
            }

        }
        log.info("【电子券用券验证】：券转赠规则验证通过==");
    }
}
